
package com.platform.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.RestResponse;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysOrgGoodsEntity;
import com.platform.modules.sys.service.SysConfigService;
import com.platform.modules.sys.service.SysOrgGoodsService;
import com.platform.modules.sys.service.SysOrgService;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 机构商品Controller
 *
 * @author
 * @date 2021-11-11 11:00:29
 */
@RestController
@RequestMapping("sys/orggoods")
public class SysOrgGoodsController extends AbstractController {
    @Autowired
    private SysOrgGoodsService sysOrgGoodsService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("sys:orggoods:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<SysOrgGoodsEntity> list = sysOrgGoodsService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询机构商品
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:orggoods:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = sysOrgGoodsService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:orggoods:info")
    public RestResponse info(@PathVariable("id") String id) {
        SysOrgGoodsEntity sysOrgGoods = sysOrgGoodsService.getById(id);

        return RestResponse.success().put("orggoods", sysOrgGoods);
    }

    /**
     * 新增机构商品
     *
     * @param sysOrgGoods sysOrgGoods
     * @return RestResponse
     */
    @SysLog("新增机构商品")
    @RequestMapping("/save")
    @RequiresPermissions("sys:orggoods:save")
    public RestResponse save(@RequestBody SysOrgGoodsEntity sysOrgGoods) {

        sysOrgGoodsService.add(sysOrgGoods);

        return RestResponse.success();
    }

    /**
     * 批量新增
     *
     * @param sysOrgGoods sysOrgGoods
     * @return RestResponse
     */
    @SysLog("批量新增")
    @RequestMapping("/addList")
    @RequiresPermissions("sys:orggoods:save")
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public RestResponse save(@RequestBody List<SysOrgGoodsEntity> sysOrgGoods) {

        List<SysOrgGoodsEntity> oldList = sysOrgGoodsService.list(
                new LambdaQueryWrapper<SysOrgGoodsEntity>().eq(SysOrgGoodsEntity::getOrgNo, this.getUser().getOrgNo())
        );
        SysOrgEntity orgEntity = sysOrgService.getById(this.getUser().getOrgNo());
        // 更新已存数据
        for (SysOrgGoodsEntity item : sysOrgGoods) {
            for (SysOrgGoodsEntity old : oldList) {
                if (item.getGoodsId().equals(old.getGoodsId())) {
                    item.setId(old.getId());
                }
            }
        }

        sysOrgGoodsService.saveOrUpdateBatch(sysOrgGoods);
        // 统计已有总数价格
        BigDecimal sumPrice = sysOrgGoodsService.orgSumPrice(this.getUser().getOrgNo());
        BigDecimal limitPrice = orgEntity.getIntegral() == null ? BigDecimal.ZERO : orgEntity.getIntegral();

        if (limitPrice.compareTo(sumPrice) < 0) {
            throw new BusinessException("可用积分不足，差：" + sumPrice.subtract(limitPrice));
        }

        return RestResponse.success();
    }

    /**
     * 修改机构商品
     *
     * @param sysOrgGoods sysOrgGoods
     * @return RestResponse
     */
    @SysLog("修改机构商品")
    @RequestMapping("/update")
    @RequiresPermissions("sys:orggoods:update")
    public RestResponse update(@RequestBody SysOrgGoodsEntity sysOrgGoods) {

        sysOrgGoodsService.update(sysOrgGoods);

        return RestResponse.success();
    }

    /**
     * 根据主键删除机构商品
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除机构商品")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:orggoods:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        sysOrgGoodsService.deleteBatch(ids);

        return RestResponse.success();
    }
}
